<?php

/**
 * 简单日志工具
 *
 * @author yuanzhen
 */
class YTool_Log
{
    static $api_log = array(
        'Friend_List',
        'Action_Item',
        'Baoku_AppContents',
        'Action_List',
        'Baoku_UserAppList',
        'Action_GiftList',
        'User_Activate',
        );

    /**
     * 调试日志
     * level 指定环境级别 001环境日志
     */
    public static function addDebugLog($logName, $v)
    {
        $desc = $v;
        if (!is_string($v))
        {
            $desc = var_export($v, true);
        }
        self::addFileLog('debug_' . $logName, $desc);
    }

    /**
     * 添加字符串日志到文件
     */
    public static function addFileLog($filename, $desc, $micro = false)
    {
        $multilogDir = "/tmp/multilog/callyummy/";
        $fn = $multilogDir . $filename;
        if (!file_exists($fn))
        {
            touch($fn);
            chmod($fn, 0666);
        }
        if (!is_writable($fn))
        {
            return;
        }
        $fp = fopen($fn, 'a');
        if ($fp)
        {
            $time = date('Y-m-d H:i:s');
            if ($micro)
            {
                $time .= "\t" . self::getMicro();
            }
            if (is_array($desc) || is_object($desc))
            {
                $desc = var_export($desc, true);
            }
            $flog = sprintf("%s\t%s\t%s\t%s\n", $time, self::getIp(), self::getPid(), $desc);
            fwrite($fp, $flog);
            fclose($fp);

            $filesize = filesize($fn);
            
            //if filesize is more than 1GB,then create a new log file using YmdHis time string suffix
            if ($filesize >= 1024 * 1024 * 1024)
            {
                $newfilename = $fn . '.' . date('YmdHis');
                rename($fn, $newfilename);
            }
        }
    }

    /**
     * 打印字符串日志到syslog
     */
    function addSysLog($tag, $message, $app = APP_NAME, $priority = LOG_NOTICE)
    {
        if (!is_string($message))
        {
            $message = var_export($message, true);
        }
        openlog(strtoupper($app), LOG_PID, LOG_LOCAL6);
        syslog($priority, $tag . "\t" . $message);
        closelog();
    }

    /**
     * 打印字符串日志到统计syslog
     */
    public static function addStatSysLog($tag, $message, $priority = LOG_NOTICE)
    {
        openlog($tag, LOG_PID, LOG_LOCAL5);
        syslog($priority, $message);
        closelog();
    }
    
    private static function getIp()
    {
        static $ip = '127.0.0.1';
        return $ip;
    }

    private static function getPid()
    {
        static $pid = NULL;
        if (NULL === $pid)
        {
            $pid = getmypid() . '.' . rand(0, 100000);
        }
        return $pid;
    }

    private static function getMicro()
    {
        list($msec, $sec) = explode(" ", microtime());
        return ((float) $msec + (float) $sec);
    }

    /**
     * 记录日志异常
     */
    public static function logException($kind, $level = "001", $ex = null)
    {
        if (!$ex)
        {
            $ex = new Exception();
        }
        self::addFileLog("exception_$kind", $ex);
        self::addFileLog("exception_$kind" . "_trace", $ex->getTraceAsString());
        throw $ex;
    }

}

?>